import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/text_style.dart';

import 'color.dart';

typedef ValueSetter =  Future<bool> Function();
// ignore: must_be_immutable
class TimerCountDownWidget extends StatefulWidget {
  late Function onTimerFinish;
  late bool isOnTap = false;
  late bool isTextColor = false;
  late Function onClickGetSmsCode;
  late ValueSetter onTap;
  int? codeStartTime = 60;
  //@ 小额鉴权页面 永远是 重新获取 文案
  bool? isSmallVertify = false;
  // late WebViewController webViewController;

  TimerCountDownWidget({
    Key? key,
    required this.onTimerFinish,
    required this.onClickGetSmsCode,
    required this.isOnTap,
    required this.isTextColor,
    required this.onTap,
    this.codeStartTime,
    this.isSmallVertify,
  }) : super(key: key);

  TimerCountDownWidgetState timerCountDownWidgetState =
      TimerCountDownWidgetState();

  @override
  State<StatefulWidget> createState() => timerCountDownWidgetState;

  startCounter() {
    // this.webViewController = webViewController;
    timerCountDownWidgetState.firstStartCounter();
  }

  textIsTap(bool isTextTap) {
    isOnTap = isTextTap;
    timerCountDownWidgetState.refreshState();
  }

  ///修改获取验证码这个字的颜色  默认是灰色，输入手机号后变成红色
  changeTextColor(bool isMobileLenght) {
    isTextColor = isMobileLenght;
    timerCountDownWidgetState.refreshState();
  }
}

class TimerCountDownWidgetState extends State<TimerCountDownWidget> {
  Timer? _timer;
  int _countdownTime = 0;
  bool firstGetSms = true;
  bool isFinish = true;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {

        widget.onTap().then((value){
          widget.isOnTap=value;
          getCode();
        });

      },
      child: Container(
        width: 84.w,
        height: 30.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius:
          BorderRadius.all(Radius.circular(5.w)),
          color: ColorManager.colorFFFFFF,
          border: Border.all(color: _countdownTime > 0 ? ColorManager.color999999 : ColorManager.color333333,width: 0.5.w)
        ),
        child: Text(
          _countdownTime > 0
              ? '$_countdownTime' + "s"
              : widget.isSmallVertify ?? false
                  ? "重新获取"
                  : firstGetSms
                      ? '获取验证码'
                      : '重新获取',
          style: _countdownTime > 0
              ? TextStyleUtils.style999999(fontSize: 14)
              : widget.isTextColor
                  ? TextStyleUtils.style333333(fontSize: 14.sp)
                  : TextStyleUtils.style333333(fontSize: 14.sp),
        ),
      ),
    );
  }

   getCode() {
      // if(widget.webViewController == null){
     if(widget.isOnTap){
       if (_countdownTime == 0) {
         setState(() {
           _countdownTime = widget.codeStartTime ?? 60;
         });
         //开始倒计时
         startCountdownTimer();
         widget.onClickGetSmsCode();
         firstGetSms = false;
       }
     }

  }

  void refreshState() {
    setState(() {});
  }

  void startCountdownTimer() {
    _timer = Timer.periodic(
        const Duration(seconds: 1),
        (Timer timer) => {
              setState(() {
                if (_countdownTime < 1) {
                  widget.onTimerFinish();
                  setState(() {
                    isFinish = true;
                  });
                  _timer!.cancel();
                  // widget.isOnTap = true;
                } else {
                  setState(() {
                    isFinish = false;
                  });
                  _countdownTime = _countdownTime - 1;
                }
              })
            });
  }

  void firstStartCounter() {
    if (widget.isOnTap) {
      if (_countdownTime == 0) {
        setState(() {
          _countdownTime = widget.codeStartTime ?? 60;
        });
        //开始倒计时
        startCountdownTimer();
        firstGetSms = false;
      }
    }
  }

  bool getIsFinish() {
    return isFinish;
  }

  @override
  void dispose() {
    super.dispose();
    if (_timer != null) {
      _timer!.cancel();
    }
  }
}
